<template>
  <el-container>
    <el-header>
      <el-col :span="8">
        <el-input v-model="queryInput" placeholder="请输入姓名搜索" />
      </el-col>

      <div>
        <el-button type="primary" @click="handleAdd">增加</el-button>
        <el-button
          type="danger"
          @click="handleListDel"
          v-if="multipleSelection.length > 0"
          >删除</el-button
        >
      </div>
    </el-header>
    <el-main>
      <el-table
        :data="tableData"
        style="width: 100%; height: 100%"
        border
        ali
        @selection-change="handleSelectionChange"
        ref="multipleTableRef"
      >
        <el-table-column align="center" type="index" label="Xh" width="55" />
        <el-table-column
          align="center"
          prop="type"
          label="紧急情况"
          :formatter="
            (row, column, cellValue, index) => {
              if (cellValue === 1) {
                return '急';
              } else if (cellValue === 2) {
                return '紧急';
              }
              return '一般';
            }
          "
          width="90"
        />
        <el-table-column
          align="left"
          header-align="center"
          prop="title"
          label="标题"
          width="200"
        />
        <el-table-column
          align="center"
          prop="name"
          label="联系人"
          width="100"
        />
        <el-table-column
          align="center"
          prop="time"
          label="截止时间"
          width="180"
        />
        <el-table-column
          align="center"
          prop="state"
          :formatter="
            (row, column, cellValue, index) => {
              if (cellValue === 1) {
                return '未完成';
              }
              return '已完成';
            }
          "
          label="状态"
          width="120"
        />
        <el-table-column align="center" fixed="right" label="操作" width="120">
          <template #default="scope">
            <!-- <el-button
              link
              type="danger"
              size="small"
              @click="handleRowDel(scope.row.id)"
              >删除</el-button
            > -->
            <el-button
              link
              type="primary"
              size="small"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--dialog 弹窗-->
      <el-dialog
        v-model="dialogFormVisible"
        :title="dialogType === 'add' ? '新增' : '编辑'"
      >
        <el-form :model="tableForm" label-width="auto">
          <el-form-item label="紧急情况">
            <el-select
              v-model="tableForm.type"
              :disabled="dialogType !== 'add'"
              placeholder="紧急情况"
            >
              <el-option label="一般" :value="0" />
              <el-option label="急" :value="1" />
              <el-option label="紧急" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="标题">
            <el-input
              v-model="tableForm.title"
              :disabled="dialogType !== 'add'"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="联系人">
            <el-input
              v-model="tableForm.name"
              :disabled="dialogType !== 'add'"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="截止时间">
            <el-date-picker
              v-model="tableForm.time"
              :disabled="dialogType !== 'add'"
              type="datetime"
              placeholder="截止时间"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select
              v-model="tableForm.state"
              autocomplete="off"
              placeholder="状态"
            >
              <el-option label="未完成" :value="0" />
              <el-option label="已完成" :value="1" />
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button type="primary" @click="dialogConfirm">确认</el-button>
          </span>
        </template>
      </el-dialog>
    </el-main>
    <el-footer>
      <el-col :span="24">
        {{ meg }}
        <el-button type="primary" @click="updateTask">检查更新</el-button>
      </el-col>
    </el-footer>
  </el-container>
</template>

<script setup>
import { ref, watch, onMounted, computed } from "vue";
import pkg from "../../../package.json";
import useApi from "../../tauri/hooks/api";
import useUpdater from "../../tauri/hooks/updater";
import useClipboard from "../../tauri/hooks/clipboard";
import useNotification from "../../tauri/hooks/notification";
const { sendMessage } = useNotification();
const api = useApi();
const { read } = useClipboard();
//数据
const queryInput = ref("");
const tableData = ref([]);
let tableDataCopy = computed(() => tableData.value);
const multipleSelection = ref([]); //多选
const dialogFormVisible = ref(false); //是否打开
const formLabelWidth = "80px"; //弹出框 标题长度
const tableForm = ref({});
const dialogType = ref("add");
//监控数据
watch(queryInput, (val) => {
  if (val.length > 0) {
    tableData.value = tableData.value.filter((item) =>
      item.title.toLowerCase().match(val.toLowerCase())
    );
  } else {
    loadPageData();
  }
});
//选择事件
const handleSelectionChange = (val) => {
  multipleSelection.value = [];
  val.forEach((item) => {
    multipleSelection.value.push(item.id);
  });
};
//添加按钮
const handleAdd = (val) => {
  tableForm.value = {};
  dialogType.value = "add";
  dialogFormVisible.value = true;
};
//编辑按钮
const handleEdit = (val) => {
  tableForm.value = val;
  dialogType.value = "edit";
  dialogFormVisible.value = true;
};

//删除一条按钮
const handleRowDel = (id) => {
  let index = tableData.value.findIndex((item) => item.id === id);
  tableData.value.splice(index, 1);
};

//删除多条
const handleListDel = () => {
  multipleSelection.value.forEach((id) => {
    handleRowDel(id);
  });
  multipleSelection.value = [];
};

//确认按钮
const dialogConfirm = () => {
  if (dialogType.value === "add") {
    api.send("/task/addTask", "POST", tableForm.value).then((res) => {
      if (res) {
        loadPageData();
      }
    });
    // tableData.value.push({
    //   id: tableData.value.length + 1,
    //   ...tableForm.value,
    // });
  } else {
    api.send("/task/updateTask", "PATCH", tableForm.value).then((res) => {
      if (res) {
        loadPageData();
      }
    });
    // let index = tableData.value.findIndex((item) => item.id === tableForm.id);
    // tableData[index] = tableForm;
  }
  dialogFormVisible.value = false;
};

const loadPageData = (not = false) => {
  api.send("/task/queryTask", "GET").then(async (res) => {
    tableData.value = res;
    if (not && tableData.value.length > 0) {
      await sendNot();
    }
  });
};

const sendNot = async () => {
  await sendMessage("提示", "您当前有代办的任务需要处理！");
};

const meg = ref("版本检测 ");

const { checkV, updater } = useUpdater();
const state = ref(false);
const updateTask = async () => {
  if (state.value) {
    await updater();
  } else {
    let res = await checkV();
    if (res) {
      meg.value = "发现新版本：" + res.meg;
      state.value = pkg.version !== res.version;
    }
  }
};

onMounted(async () => {
  loadPageData(true);
  console.log(await read());
});
</script>

<style lang="scss" scoped>
.el-container {
  width: 100%;
  height: 100%;
  padding: 10px;
  background-color: #f0f2f5;
  .el-header {
    background-color: #b3c0d1;
    color: #333;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
  .el-main {
    background-color: #b3c0d1;
    color: #333;
    text-align: center;
    padding: 10px;
  }
  .el-footer {
    background-color: #b3c0d1;
    color: #333;
    text-align: center;
    padding: 10px;
  }
}
</style>
